﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseModule;
using System.IO;

namespace EMC
{
    public class UserManagement
    {
        private DAO dbConn;

        private databaseConfiguration config;

        public UserManagement()
        {
            String szConfig = Directory.GetCurrentDirectory() + "\\db_settings.xml";
            this.config = new databaseConfiguration(szConfig);
            dbConn = dbFactory.newDB(config.dbType, config.dbConnectionString);
            //dbConn.SetVerboseMode(true);
        }

        public bool adduser(string userid, string first_name, string last_name, string pass, string phone, string email)
        {
            KeyValues fieldVals = new KeyValues();

            fieldVals.Add(config.tableUsers_ID, userid);
            fieldVals.Add(config.tableUsers_FirstName, first_name);
            fieldVals.Add(config.tableUsers_LastName, last_name);
            fieldVals.Add(config.tableUsers_Password, pass);
            fieldVals.Add(config.tableUsers_Phone, phone);
            fieldVals.Add(config.tableUsers_Email, email);


            //dbConn.SetVerboseMode(true);
            return dbConn.InsertRow(config.tableUsers, fieldVals);
            //Console.WriteLine(dbConn.GetLastError());
        }
        public bool delUser(string userid)
        {

            //Delete the user's files
            bool files = false;
            KeyValues userFiles = new KeyValues();
            userFiles.Add(config.tableFiles_UserID, userid);
            files = dbConn.DeleteRows(config.tableFiles, userFiles);

            //Delete the user's friends list
            bool friends = false;
            KeyValues userFriends = new KeyValues();
            userFriends.Add(config.tableFriends_UserID1, userid, ConditionMode.ModeOr);
            userFriends.Add(config.tableFriends_UserID2, userid);
            friends = dbConn.DeleteRows(config.tableFriends, userFriends);

            //Delete the user 
            bool user = false;
            KeyValues userDetails = new KeyValues();
            userDetails.Add(config.tableUsers_ID, userid);
            user = dbConn.DeleteRows(config.tableUsers, userDetails);
            if (files == true || friends == true || user == true)
            {
                return true;
            }
            return false;
        }

        public void setPassword(string userid, string pass)
        {
            KeyValues fieldVals = new KeyValues();
            KeyValues keyVals = new KeyValues();

            keyVals.Add(config.tableUsers_Password, pass);
            fieldVals.Add(config.tableUsers_ID, userid);
            //dbConn.SetVerboseMode(true);
            dbConn.UpdateRows(config.tableUsers, keyVals, fieldVals);
        }

        // Assuming that for unentered fields the an empty string is passed
        public bool editUser(string userid, string first_name, string last_name, string pass, string phone, string email)
        {

            KeyValues fieldVals = new KeyValues();
            KeyValues keyVals = new KeyValues();

            if (first_name != null)
            {

                keyVals.Add(config.tableUsers_FirstName, first_name);

            }
            if (last_name != null)
            {

                keyVals.Add(config.tableUsers_LastName, last_name);

            }

            if (pass != null)
            {

                keyVals.Add(config.tableUsers_Password, pass);

            }
            if (phone != null)
            {

                keyVals.Add(config.tableUsers_Phone, phone);

            }
            if (email != null)
            {

                keyVals.Add(config.tableUsers_Email, email);

            }
            fieldVals.Add(config.tableUsers_ID, userid);
            //dbConn.SetVerboseMode(true);
            return dbConn.UpdateRows(config.tableUsers, keyVals, fieldVals);

        }

        public bool userExists(string userid)
        {
            KeyValues fieldVals = new KeyValues();
            fieldVals.Add(config.tableUsers_ID, userid);
            string dbuser = dbConn.GetValue(config.tableUsers, config.tableUsers_ID, fieldVals);
            if (dbuser != null && dbuser.ToLower() == userid.ToLower())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public Dictionary<string, string> GetUser(string userID)
        {
            Dictionary<string, string> userDetais = null;
            if (userExists(userID))
            {
                KeyValues keyVals = new KeyValues();
                keyVals.Add(config.tableUsers_ID, userID);
                userDetais = dbConn.GetRow(config.tableUsers, new string[] { config.tableUsers_ID, config.tableUsers_FirstName, config.tableUsers_LastName, config.tableUsers_Email, config.tableUsers_Phone, config.tableUsers_Email }, keyVals);
            }


            //Return the details
            return userDetais;
        }

        public string GetUserPassword(string userID)
        {

            KeyValues keyVals = new KeyValues();
            keyVals.Add(config.tableUsers_ID, userID);

            return dbConn.GetValue(config.tableUsers, config.tableUsers_Password, keyVals);

        }


        public List<string> ListUsers()
        {
            List<Dictionary<string, string>> userList = dbConn.GetRows(config.tableUsers, new string[] { config.tableUsers_ID }, null);
            List<string> simplifiedList = userList.ConsolidateValues(config.tableUsers_ID);
            return simplifiedList;
        }

        public bool authenticateUser(string userID, string password)
        {
            if (userExists(userID))
            {
                KeyValues keyVals = new KeyValues();
                keyVals.Add(config.tableUsers_ID, userID);
                string userPass = dbConn.GetValue(config.tableUsers, config.tableUsers_Password, keyVals);
                if (password == userPass)
                {
                    return true;
                }
            }

            return false;
        }
    }
}
